package com.lse_api.utils.pdfutil.stringutil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * string util
 *
 * @author 祁雪
 */
public class StringUtils {

    /**
     * 字符串格式化
     * 例如: xxx{0}xxx{1}xxx...........
     *
     * @param string
     * @param strings
     * @return
     */
    public static String format(String string, String... strings) {

        if (strings == null || isEmpty(string)) {
            return string;
        }
        for (int i = 0; i < strings.length; i++) {
            string = string.replaceAll("\\{" + i + "}", strings[i]);
        }

        return string;
    }

    /**
     * 字符串格式化按照 map.k值
     *
     * @param string
     * @param map
     * @return
     */
    public static String format(String string, Map<String, String> map) {

        if (map == null || isEmpty(string)) {
            return string;
        }

        for (Map.Entry<String, String> entry : map.entrySet()) {
            string = string.replaceAll("\\{" + entry.getKey() + "}",
                    isEmpty(entry.getValue()) ? "" : entry.getValue());
        }

        return string;
    }

    public static boolean isEmpty(String string) {

        if (string == null || "".equals(string.trim()) || string.trim().length() == 0) {
            return true;
        }
        return false;
    }

    public static boolean isNumber(String string) {

        if (isEmpty(string)) {
            return false;
        }
        for (int i = 0; i < string.length(); i++) {
            if (!String.valueOf(string.charAt(i)).matches("[0-9]")) {
                return false;
            }
        }
        return true;
    }

    /**
     * 字符串是否含有 strings 中的某一个
     *
     * @param string
     * @param strings
     * @return
     */
    public static boolean having(String string, String... strings) {

        if (strings == null || isEmpty(string)) {
            return false;
        }

        for (String key : strings) {
            Pattern pattern = Pattern.compile(key, Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(string);
            if (matcher.find()) {
                return true;
            }
        }

        return false;
    }

    /**
     * 模糊匹配list中包含key的值 返回list
     *
     * @param list
     * @param key
     * @return
     */
    public static List<String> having(List<String> list, String key) {

        if (list == null || isEmpty(key)) {
            return list;
        }

        Pattern pattern = Pattern.compile(key, Pattern.CASE_INSENSITIVE);
        List<String> newList = new ArrayList<>();

        for (String s : list) {
            Matcher matcher = pattern.matcher(s);
            if (matcher.find()) {
                newList.add(s);
            }
        }

        return newList;
    }

    /**
     * 字符串清除 strings 中内容
     *
     * @param string
     * @param strings
     * @return
     */
    public static String remove(String string, String... strings) {

        if (strings == null || isEmpty(string)) {
            return string;
        }

        for (String s : strings) {
            string = string.replaceAll(s, "");
        }

        return string;

    }

    /**
     * list 去重
     *
     * @param list
     * @return
     */
    public static List<String> duplicateRemoval(List<String> list) {
        return list.stream().distinct().collect(Collectors.toList());
    }

    /**
     * 多个list去重 重复数据不会出现在返回数据中
     *
     * @param lists
     * @return
     */
    public static List<String> duplicateRemoval(List<String>... lists) {
        List<String> listA = lists[0];
        if (lists.length == 1) {
            return duplicateRemoval(listA);
        }
        // 取出重复值
        List<String> stringList = listA.stream().filter(value -> {
            List<Boolean> list = new ArrayList<>();
            for (int i = 1; i < lists.length; i++) {
                if (lists[i].contains(value)) {
                    list.add(true);
                } else {
                    list.add(false);
                }
            }
            if (list.contains(false)) {
                return false;
            }
            return true;
        }).collect(Collectors.toList());

        List<String> list = new ArrayList<>();
        Arrays.stream(lists).forEach(listx -> {
            list.addAll(listx.stream().filter(value -> !stringList.contains(value)).collect(Collectors.toList()));
        });

        return list;
    }

    public static void main(String[] args) {
        System.out.println(" ".trim().length());
    }

}
